/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "M2File.h"

namespace PQGameCore
{
	M2File::M2File(PQEngine::FileBase* file)
		: _buf(0), _ok(false), _header(0),_globSeqs(0),_bones(0),_vertices(0),_colors(0),_transps(0),
		_m2TexDefs(0),_m2Anims(0),_m2TexAnims(0),_m2ParticleEmitters(0),_m2RibbonEmitters(0),_m2Cameras(0),
		_m2Lights(0),_m2RenderFlag(0),_m2TexLookup(0),_m2TexAnimLookup(0),_m2TexUnitLookup(0)
	{
		assert(file);
		
		_fileName=file->getFileName();
		_fileSize=file->getSize();
		_buf=new char[_fileSize];
		file->read(_buf,_fileSize);

		parse();
		
	}

	M2File::~M2File(void)
	{
		if(_buf){
			delete _buf;
		}
	}

	bool M2File::parse()
	{
		
		_header=(M2Header*)_buf;

		if(strncmp(_header->id,"MD20",4)!=0){
			_ok=false;
			return false;
		}

		if (_header->version[0] != 4 && _header->version[1] != 1 && _header->version[2] != 0 && _header->version[3] != 0) {
			_ok=false;
			return false;
		}

		if (_header->nameOffset != 304 && _header->nameOffset != 320) {
			//file maybe corrupted
			_ok=false;
			return false;
		}

		if (_header->nGlobSeqs>0) {
			_globSeqs=(unsigned int*)(_buf+_header->globSeqOffset);
		}

		if (_header->nBones>0) {
			_bones=(M2Bone*)(_buf+_header->bonesOffset);
		}

		if(_header->nKeyBoneLookup>0){
			_m2KeyBoneLookup=(short*)(_buf+_header->keyBoneLookupOffset);
		}

		if(_header->nAnimLookup>0){
			_m2AnimLookup=(short*)(_buf+_header->animLookupOffset);
		}

		if(_header->nVertices>0){
			_vertices = (M2Vertex*)(_buf + _header->verticesOffset);
		}
	
		if (_header->nTex>0) {
			_m2TexDefs = (M2TexDef*)(_buf + _header->texOffset);
		}

		// init _colors
		if (_header->nColors>0) {
			_colors=(M2Color*)(_buf+_header->colorsOffset);
		}

		if (_header->nTransps>0) {
			_transps=(M2Transp*)(_buf+_header->transpOffset);
		}
		if(_header->nRenderFlag>0){
			_m2RenderFlag=(M2RenderFlag*)(_buf+_header->renderFlagOffset);
		}
		if(_header->nTexLookup>0){
			_m2TexLookup = (unsigned short*)(_buf + _header->texLookupOffset);
		}
		if(_header->nTexAnimLookup>0){
			_m2TexAnimLookup = (unsigned short*)(_buf + _header->texAnimLookupOffset);
		}
		if(_header->nTexUnitLookup>0){
			_m2TexUnitLookup = (short*)(_buf+ _header->texUnitLookupOffset);
		}

		if (_header->nAnims > 0) {
			_m2Anims = (M2Animation*)(_buf+_header->animsOffset);
		}

		if (_header->nTexAnims) {
			_m2TexAnims = (M2TexAnim*)(_buf+_header->texAnimsOffset);
		}

		if (_header->nParticleEmitters>0) {
			_m2ParticleEmitters=(M2ParticleEmitter*)(_buf+_header->particleEmittersOffset);
		}

		// ribbons
		if (_header->nRibbonEmitters>0) {
			_m2RibbonEmitters=(M2RibbonEmitter*)(_buf+_header->ribbonEmittersOffset);
		}

		if(_header->nCameras>0){
			_m2Cameras=(M2Camera*)(_buf+_header->cameraOffset);
		}

		if(_header->nLights>0){
			_m2Lights=(M2Light*)(_buf+_header->lightsOffset);
		}

		if(_header->nTranspLookup>0){
			_m2TranspLookup=(short*)(_buf+_header->transpLookupOffset);
		}
		_ok=true;
		return true;
	}


}
